package controller;

import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;

import entity.Avatar;
import entity.Entity;

import view.overlay.MapOverlay;

import map.Direction;
import map.GameWorld;
import map.Position;
import model.Observer;
import model.Subject;

public class MapController extends Controller implements Observer {
	
	private MapOverlay overlay;
	private Position center;
	
	private Entity mEntity;
	
	public MapController(MapOverlay overlay) {
		
		GameWorld world = GameWorld.getInstance();
		Avatar avatar = world.getAvatar();
		avatar.register(this);
		
		mEntity = avatar;
		
		this.overlay = overlay;
		this.center = overlay.getCenter();
	}
	

	
	@Override
	public List<GameControl> getKeyControls() {
		
		List<GameControl> controls = new ArrayList<GameControl>();
		
		
		// Move North
		controls.add(new GameControl(KeyEvent.VK_Y, "MOVE_MAP_N", "Look North") {

			@Override
			protected void execute() {
				center = overlay.getCenter();
				center.update(Direction.NORTH);
			}
			
		});
		
		// Move Northeast
		controls.add(new GameControl(KeyEvent.VK_U, "MOVE_MAP_NE", "Look North-East") {

			@Override
			protected void execute() {
				center = overlay.getCenter();
				center.update(Direction.NORTHEAST);
			}
			
		});
		
		// Move Northwest
		controls.add(new GameControl(KeyEvent.VK_T, "MOVE_MAP_NW", "Look North-West") {

			@Override
			protected void execute() {
				center = overlay.getCenter();
				center.update(Direction.NORTHWEST);
			}
			
		});
		
		// Move South
		controls.add(new GameControl(KeyEvent.VK_N, "MOVE_MAP_S", "Look South") {

			@Override
			protected void execute() {
				center = overlay.getCenter();
				center.update(Direction.SOUTH);
			}
			
		});
		
		// Move Southeast
		controls.add(new GameControl(KeyEvent.VK_M, "MOVE_MAP_SE", "Look South-East") {

			@Override
			protected void execute() {
				center = overlay.getCenter();
				center.update(Direction.SOUTHEAST);
			}
			
		});
		
		// Move Southwest
		controls.add(new GameControl(KeyEvent.VK_B, "MOVE_MAP_SW", "Look South-West") {

			@Override
			protected void execute() {
				center = overlay.getCenter();
				center.update(Direction.SOUTHWEST);
			}
			
		});
		
		
		// Return the controls
		return (controls);
	}
	
	public void update(Subject subject) {
		
		if (subject.equals(mEntity)) {
			overlay.setCenter(new Position(mEntity.getPosition().getX(), mEntity.getPosition().getY()));
		}

		
	}
	
	@Override
	public void reset() {
		// nothing
	}
}
